<?php

/**
 * This is the model class for table "tbl_businessCase".
 *
 * The followings are the available columns in table 'tbl_businessCase':
 * @property integer $id
 * @property string $productName
 * @property string $companyName
 * @property string $description
 * @property string $details_customerSegment
 * @property string $details_valueProposition
 * @property string $details_revenueStreams
 * @property string $details_businessResources
 * @property integer $business_id
 * @property integer $businessModel_id
 *
 * The followings are the available model relations:
 * @property Business $business
 * @property Businessmodel $businessModel
 */
class BusinessCase extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return BusinessCase the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_businessCase';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('business_id, businessModel_id', 'required'),
			//array('business_id, businessModel_id', 'numerical', 'integerOnly'=>true),
			array('productName, companyName', 'length', 'max'=>256),
			array('description, details_customerSegment, details_valueProposition, details_revenueStreams, details_businessResources', 'length', 'max'=>1000),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, productName, companyName, description, details_customerSegment, details_valueProposition, details_revenueStreams, details_businessResources, business_id, businessModel_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'business' => array(self::BELONGS_TO, 'Business', 'business_id'),
			'businessModel' => array(self::BELONGS_TO, 'Businessmodel', 'businessModel_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'productName' => 'Product Name',
			'companyName' => 'Company Name',
			'description' => 'Description',
			'details_customerSegment' => 'Details Customer Segment',
			'details_valueProposition' => 'Details Value Proposition',
			'details_revenueStreams' => 'Details Revenue Streams',
			'details_businessResources' => 'Details Business Resources',
			'business_id' => 'Business',
			'businessModel_id' => 'Business Model',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('productName',$this->productName,true);
		$criteria->compare('companyName',$this->companyName,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('details_customerSegment',$this->details_customerSegment,true);
		$criteria->compare('details_valueProposition',$this->details_valueProposition,true);
		$criteria->compare('details_revenueStreams',$this->details_revenueStreams,true);
		$criteria->compare('details_businessResources',$this->details_businessResources,true);
		$criteria->compare('business_id',$this->business_id);
		$criteria->compare('businessModel_id',$this->businessModel_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
// return CForm for wizard-behavior
	public function getForm() {
		return new CForm(array(
			'showErrorSummary'=>true,
			'attributes'=>array('id'=>'bcase'),
			'elements'=>array(
				'productName'=>array(
					'type'=>'text',
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				'companyName'=>array(
					'type'=>'text',
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				'description'=>array(
					'type'=>'textarea',
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),				
				'details_customerSegment'=>array(
					'type'=>'textarea',
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				'details_valueProposition'=>array(
					'type'=>'textarea',
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				'details_revenueStreams'=>array(
					'type'=>'textarea',
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				'details_businessResources'=>array(
					'type'=>'textarea',
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
			),
			'buttons'=>array(
				'submit'=>array(
					'type'=>'submit',
					'label'=>'Next'
				)
			)
		), $this);
	}
	
public function isRecordExists($businessCase)
	{		

		$sqlBusinessCase = 'SELECT tbl_businessCase.id
			FROM tbl_businessCase
			WHERE tbl_businessCase.companyName = :companyName AND
			tbl_businessCase.businessDescription = :businessDescription AND
			tbl_businessCase.argumentation = :argumentation AND
			tbl_businessCase.business_id = :business_id AND
			tbl_businessCase.businessModel_id = :businessModel_id' ;
		
		$dataBusinessCase=new CSqlDataProvider($sqlBusinessCase, array(
    		'params'=>array(':companyName'=>$businessCase->companyName,
							':businessDescription'=>$business->businessDescription,
							':argumentation'=>$business->argumentation,
							':business_id'=>$business->business_id,
							':businessModel_id'=>$business->businessModel_id),
    		'sort'=>array(
        		'attributes'=>array(
            		 'id'),
  			  ),
    		'pagination'=>array(
        	'pageSize'=>10,
    		),
			));
		
		$dataBusinessCase = $dataBusinessCase->getData();
		
		return $dataBusinessCase[0]['id'];				
		
	}
}